package entity

import (
	"blog/app/dto"
	"blog/app/models"
	"blog/config"
	"blog/utils"
	"errors"
	"fmt"
	"gorm.io/gorm"
)

// LoginCheck 检查登录用户名和密码是否正确
func LoginCheck(param dto.UserLoginParams) (error, int) {
	var dbUser models.UserBasic
	if err := utils.MySqlDB.Model(&dbUser).Where("user_name = ?", param.UserName).Find(&dbUser).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			fmt.Println("数据不存在", err)
			return err, config.LoginFail
		} else {
			fmt.Println("其他错误", err)
			return nil, config.LoginFail
		}
	}
	fmt.Println("查询结果没有错误", dbUser)
	if dbUser.ID > 0 {
		return nil, config.SUCCESS
	} else {
		return nil, config.LoginFail
	}
}

func LoginCheckParam(param dto.UserLoginPost) (error, int) {
	var dbUser models.UserBasic
	if err := utils.MySqlDB.Model(&dbUser).Where("user_name = ?", param.Params.UserName).First(&dbUser).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			fmt.Println("数据不存在")
			return err, config.LoginFail
		} else {
			fmt.Println("其他错误", err)
			return nil, config.LoginFail
		}
	}
	return nil, config.SUCCESS
}

func LoginCheck3(param dto.UserLoginPost) (error, int) {
	var dbUser models.UserBasic
	err := utils.MySqlDB.Where("user_name = ?", param.Params.UserName).First(&dbUser).Error
	if err != nil {
		fmt.Println("查询失败", err)
		return nil, config.ERROR
	} else {
		if dbUser.ID > 0 {
			return nil, config.SUCCESS
		} else {
			return nil, config.ERROR
		}
	}
}
